package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlnemfdDataOnDemand.
 */
@Component
@Configurable
@RooDataOnDemand(entity = Ttlnemfd.class)
public class TtlnemfdDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlnemfd> data;

	/**
	 * Gets the new transient ttlnemfd.
	 *
	 * @param index the index
	 * @return the new transient ttlnemfd
	 */
	public Ttlnemfd getNewTransientTtlnemfd(int index) {
        Ttlnemfd obj = new Ttlnemfd();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodFamilia(obj, index);
        setCodSubfamil(obj, index);
        setDesProd(obj, index);
        setXtiVenta(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlnemfd obj, int index) {
        TtlnemfdPK embeddedIdClass = new TtlnemfdPK(new Integer(index), new Integer(index).shortValue());
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlnemfd obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlnemfd obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod familia.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodFamilia(Ttlnemfd obj, int index) {
        Character codFamilia = new Character('N');
        obj.setCodFamilia(codFamilia);
    }

	/**
	 * Sets the cod subfamil.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodSubfamil(Ttlnemfd obj, int index) {
        Short codSubfamil = new Integer(index).shortValue();
        obj.setCodSubfamil(codSubfamil);
    }

	/**
	 * Sets the des prod.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesProd(Ttlnemfd obj, int index) {
        String desProd = "desProd_" + index;
        if (desProd.length() > 60) {
            desProd = desProd.substring(0, 60);
        }
        obj.setDesProd(desProd);
    }

	/**
	 * Sets the xti venta.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXtiVenta(Ttlnemfd obj, int index) {
        Character xtiVenta = new Character('N');
        obj.setXtiVenta(xtiVenta);
    }

	/**
	 * Gets the specific ttlnemfd.
	 *
	 * @param index the index
	 * @return the specific ttlnemfd
	 */
	public Ttlnemfd getSpecificTtlnemfd(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlnemfd obj = data.get(index);
        return Ttlnemfd.findTtlnemfd(obj.getId());
    }

	/**
	 * Gets the random ttlnemfd.
	 *
	 * @return the random ttlnemfd
	 */
	public Ttlnemfd getRandomTtlnemfd() {
        init();
        Ttlnemfd obj = data.get(rnd.nextInt(data.size()));
        return Ttlnemfd.findTtlnemfd(obj.getId());
    }

	/**
	 * Modify ttlnemfd.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlnemfd(Ttlnemfd obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlnemfd.findTtlnemfdEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlnemfd' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Ttlnemfd>();
        for (int i = 0; i < 10; i++) {
            Ttlnemfd obj = getNewTransientTtlnemfd(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
